﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarchEstate.Manager.BusinesEntity;
using MarchEstate.Manager.Dao.Room;
using CoreFrame.Core.Orm;
using System.Collections;

namespace MarchEstate.Manager.ObjectModel.Room
{
    public class RoomManager : AbstractRoom, IJoinManager
    {
        private AreaRoomDao areaRoomDao = null;

        public RoomManager()
        {

        }
        public override void InitDao()
        {
            base.InitDao();
            areaRoomDao = NewDao<AreaRoomDao>();
        }


        public override bool Save()
        {
            if (this.RoomId == null || this.RoomId=="")
            {
                if (areaRoomDao.ExistRoomModel(this.RoomNumber))
                {
                    throw new Exception("输入的房间编码[" + this.RoomNumber + "]已存在，请重新输入！");  
                }
                return areaRoomDao.AddRoom(this) > 0;
            }
            else
            {
                return areaRoomDao.UpdateRoom(this) > 0;
            }
            return true;
        }

        public override bool Save(List<AbstractRoom> roomList)
        {
            for (int i = 0; i < roomList.Count;i++)
            {
                AbstractRoom room = roomList[i];
                if (room.RoomId == null || room.RoomId == "")
                {
                    if (areaRoomDao.ExistRoomModel(room.RoomNumber))
                    {
                        throw new Exception("输入的房间编码["+room.RoomNumber+"]已存在，请重新输入！");
                    }
                    areaRoomDao.AddRoom(room);
                }
                else
                {
                     areaRoomDao.UpdateRoom(room);
                }
              
            }
            return true;
        }

        public override bool Delete(string _roomId)
        {
            return areaRoomDao.DeleteRoom(_roomId);
        }

        public override AbstractRoom GetRoom(string _roomNumber)
        {
            return areaRoomDao.GetRoom(_roomNumber);
        }

        public override System.Data.DataTable GetRoomList(object value, PageInfo page)
        {
            string  use_type;
            string useStatus;
            int saleflag = Convert.ToInt32(((Hashtable)value)["saleflag"]);

            if(((Hashtable)value)["use_type"].ToString().Trim()=="")
            {
                use_type = "(1,2,3)";
            }
             else
            {
                  use_type="("+((Hashtable)value)["use_type"]+")";
            }
            if (saleflag == -1)
            {

                useStatus = "(1,2,3,4)";
            }
            else if (saleflag == 0)//已售
            {
                useStatus = "(3,4)";
            }
            else
            {
                useStatus = "(1,2)";
            }

            return areaRoomDao.GetRoomList(((Hashtable)value)["areacode"].ToString(), ((Hashtable)value)["buildingCode"].ToString(), use_type, useStatus,((Hashtable)value)["roomname"].ToString(), page);
        }


        public override List<AbstractRoom> GetRoomList(string _buildingCode, int _useType)
        {
            throw new NotImplementedException();
        }

        #region IJoinManager 成员

        public bool Join(System.Collections.Hashtable value)
        {
            string srccId = value["srccId"].ToString();
            return areaRoomDao.JoinRoom(srccId);
        }

        public bool CancelJoin(string srccId)
        {
            return areaRoomDao.CancelJoinRoom(srccId);
        }

        #endregion

        
    }
}
